package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.RepoGoodsSpecEntity;
import cn.mumzone.pos.model.RepoListGoodsEntity;
import cn.mumzone.pos.ui.widgets.util.StringSpanUtils;

/**
 * Created by qiuzhenhuan on 2019/7/31.
 * 搜索结果列表适配器 && 盘点单列表展示
 */

public class ADA_StockCheck extends CommonAdapter<RepoListGoodsEntity> {

    private Context context;
    private boolean canDelete;

    public ADA_StockCheck(Context context, boolean canDelete) {
        super(context);
        this.context = context;
        this.canDelete = canDelete;
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_check_stock_list;
    }

    @Override
    protected void convert(final ViewHolder holder, final RepoListGoodsEntity repoGoodsEntity, final int position) {

        holder.setText(R.id.tv_good_name, StringUtils.get(repoGoodsEntity.getGoodsName()));
        holder.setText(R.id.tv_good_spec, getFormatGoodsSpec(repoGoodsEntity.getParameter()));

        // 是否选中
        holder.setVisible(R.id.iv_checked, repoGoodsEntity.isChecked());

        if (canDelete) {
            // 盘点页面，可操作删除条目，不展示选中
            holder.setVisible(R.id.iv_delete, true);
            holder.setVisible(R.id.iv_checked, false);
            holder.setOnClickListener(R.id.iv_delete, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 点击删除以后
                    if (mListener != null) {
                        mListener.OnClicked(position - 1, repoGoodsEntity);
                    }
                }
            });
        } else {
            // 搜索结果页面，可以操作选中，不可操作删除
            holder.setVisible(R.id.iv_delete, false);
            holder.getConvertView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    repoGoodsEntity.setChecked(!repoGoodsEntity.isChecked());
                    holder.setVisible(R.id.iv_checked, repoGoodsEntity.isChecked());
                    checkAllStatus();
                }
            });
        }

    }

    private void checkAllStatus() {
        if (mOnItemUncheckedListener != null) {
            mOnItemUncheckedListener.onItemUnchecked();
        }
    }

    private String getFormatGoodsSpec(List<RepoGoodsSpecEntity> goodsSpec) {
        StringBuilder builder = new StringBuilder();
        if (!ListUtil.isListEmpty(goodsSpec)) {
            for (int index = 0; index < goodsSpec.size(); index++) {
                RepoGoodsSpecEntity specBean = goodsSpec.get(index);
                builder.append(specBean.getParamValue());
                if (index < goodsSpec.size() - 1) {
                    builder.append("，");
                }
            }
        }
        return StringSpanUtils.init().addForegroundColorHighLight(mContext.getResources().getColor(R.color.weak_text_color), "规格：")
                .addStr(StringUtils.get(builder.toString(), "无")).build().toString();
    }

    public void updateCheckAllStatus(boolean isChecked) {
        for (RepoListGoodsEntity goodsEntity : getDataList()) {
            goodsEntity.setChecked(isChecked);
        }
        notifyDataSetChanged();
    }

    public List<RepoListGoodsEntity> getSelectedItems() {
        ArrayList<RepoListGoodsEntity> list = new ArrayList<>();
        for (RepoListGoodsEntity goodsEntity : getDataList()) {
            if (goodsEntity.isChecked()) {
                list.add(goodsEntity);
            }
        }
        return list;
    }

    public int getSelectedCount() {
        int totalCount = 0;
        for (RepoListGoodsEntity goodsEntity : getDataList()) {
            if (goodsEntity.isChecked()) {
                totalCount++;
            }
        }
        return totalCount;
    }

    public interface OnItemDeleteClickedListener {
        void OnClicked(int position, RepoListGoodsEntity repoGoodsEntity);
    }

    private OnItemDeleteClickedListener mListener;

    public void setOnItemDeleteClickedListener(OnItemDeleteClickedListener mListener) {
        this.mListener = mListener;
    }

    public interface OnItemUncheckedListener {
        void onItemUnchecked();
    }

    private OnItemUncheckedListener mOnItemUncheckedListener;

    public void setOnItemUncheckedListener(OnItemUncheckedListener mListener) {
        this.mOnItemUncheckedListener = mListener;
    }
}
